package com.example.flower_shop.service.adm.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.flower_shop.common.PageResult;
import com.example.flower_shop.entity.StoreOrder;
import com.example.flower_shop.entity.StoreOrderInfo;
import com.example.flower_shop.enums.OrderStatus;
import com.example.flower_shop.enums.PayStatus;
import com.example.flower_shop.enums.RefundStatus;
import com.example.flower_shop.exception.exception;
import com.example.flower_shop.mapper.StoreOrderInfoMapper;
import com.example.flower_shop.mapper.StoreOrderMapper;
import com.example.flower_shop.service.adm.AdminOrderService;
import com.example.flower_shop.vo.order.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AdminOrderServiceImpl implements AdminOrderService {
    @Resource
    private StoreOrderMapper orderMapper;
    @Resource
    private StoreOrderInfoMapper orderInfoMapper;

    @Override
    public List<AdmOrderDetailResp> orderDetail(Long orderId) {
        LambdaQueryWrapper<StoreOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreOrder::getId, orderId);
        StoreOrder order = orderMapper.selectOne(wrapper);
        if (order == null) {
            throw new exception("订单不存在!");
        }
        LambdaQueryWrapper<StoreOrderInfo> productsQuery = new LambdaQueryWrapper<>();
        productsQuery.eq(StoreOrderInfo::getOrderNo, order.getOrderNo());
        List<StoreOrderInfo> orderInfos = orderInfoMapper.selectList(productsQuery);
        return BeanUtil.copyToList(orderInfos, AdmOrderDetailResp.class);
    }

    @Override
    public PageResult<AdmOrderPageResp> orderList(AdmOrderPageReq req) {
        Page<AdmOrderPageResp> page = new Page<>(req.getPage(), req.getSize());
        page = orderMapper.admPage(page, req);
        return PageResult.of(page.getTotal(), page.getCurrent(), page.getSize(), page.getRecords());
    }

    @Override
    public void deliver(DeliverOrderReq req) {
        LambdaQueryWrapper<StoreOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreOrder::getId, req.getOrderId())
                .eq(StoreOrder::getPaid, PayStatus.PAID.getCode())
                .eq(StoreOrder::getStatus, OrderStatus.WAITING_TO_SEND.getCode())
                .eq(StoreOrder::getRefundStatus, RefundStatus.NOT_REFUND.getCode());
        StoreOrder order = orderMapper.selectOne(wrapper);
        if (order == null) {
            throw new exception("订单不存在或状态异常!");
        }
        LambdaUpdateWrapper<StoreOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StoreOrder::getId, req.getOrderId())
                .eq(StoreOrder::getPaid, PayStatus.PAID.getCode())
                .eq(StoreOrder::getStatus, OrderStatus.WAITING_TO_SEND.getCode())
                .eq(StoreOrder::getRefundStatus, RefundStatus.NOT_REFUND.getCode());
        updateWrapper.set(StoreOrder::getStatus, OrderStatus.WAITING_TO_RECEIVE.getCode())
                .set(StoreOrder::getDeliveryCode, req.getDeliveryCode())
                .set(StoreOrder::getDeliveryName, req.getDeliveryName())
                .set(StoreOrder::getDeliveryType, req.getDeliveryType())
                .set(StoreOrder::getDeliveryId, req.getDeliveryId());
        if (orderMapper.update(updateWrapper) < 1) {
            throw new exception("发货失败!");
        }
    }

    @Override
    public void handleRefund(HandleOrderReq req) {
        LambdaQueryWrapper<StoreOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreOrder::getId, req.getOrderId())
                .eq(StoreOrder::getPaid, PayStatus.PAID.getCode())
                .eq(StoreOrder::getRefundStatus, RefundStatus.APPEALING.getCode());
        StoreOrder order = orderMapper.selectOne(wrapper);
        if (order == null) {
            throw new exception("订单不存在或状态异常!");
        }
        LambdaUpdateWrapper<StoreOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StoreOrder::getId, req.getOrderId())
                .eq(StoreOrder::getPaid, PayStatus.PAID.getCode())
                .eq(StoreOrder::getRefundStatus, RefundStatus.APPEALING.getCode());
        updateWrapper.set(StoreOrder::getRefundStatus, req.getIsAgree() ?
                RefundStatus.REFUNDED.getCode() : RefundStatus.NOT_REFUND.getCode());
        if (orderMapper.update(updateWrapper) < 1) {
            throw new exception("处理退款失败!");
        }
    }
}
